package cn.stylefeng.guns.modular.api;


import cn.stylefeng.guns.core.common.exception.BankcardException;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.exception.NumberErrorException;
import cn.stylefeng.guns.core.common.util.ObjectUtil;
import cn.stylefeng.guns.core.util.HttpClientUtil;
import cn.stylefeng.guns.modular.system.entity.BankCard;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HttpClientService {

    protected final static Logger logger = LoggerFactory.getLogger(HttpClientService.class);

    /**
     * 通过userId , carId 去访问service 租车
     * @param userId
     * @param carId
     * @return
     */
    public String toRentCar(Long userId , Long carId, Date returnDate){
        if (ToolUtil.isEmpty(userId) && ToolUtil.isEmpty(carId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/clientTest";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userId",userId.toString()));
            nameValuePairs.add(new BasicNameValuePair("carId",carId.toString()));
            nameValuePairs.add(new BasicNameValuePair("returnDate",returnDate.toString()));

            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw  new BankcardException();
            return jsonObject.toString();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


    public void bingDingCard(BankCard bankCard, Long userId) {
        if (ToolUtil.isEmpty(userId) && ToolUtil.isEmpty(bankCard)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/bingDingCard";
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("bankcard", bankCard.toString()));
            nameValuePairs.add(new BasicNameValuePair("userId", userId.toString()));
            String result = HttpClientUtil.sendPost(url, nameValuePairs);
            logger.info("result: = " + result);
            JSONObject jsonObject = JSON.parseObject(result);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new BankcardException();
        } catch (BankcardException b){
            throw new BankcardException();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /*public void addMoney(Integer money, Long userId) {
        if (ToolUtil.isEmpty(userId) && ToolUtil.isEmpty(money)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/addMoney";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userId",userId.toString()));
            nameValuePairs.add(new BasicNameValuePair("money",money.toString()));
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }*/

    public void editCard(BankCard bankCard) {
        if (ToolUtil.isEmpty(bankCard)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/editCard";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("bankcard",bankCard.toString()));
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new BankcardException();
        } catch (BankcardException b){
            throw new BankcardException();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public void deleteCard(Long userId) {
        String url = "http://localhost:6262/bank/deleteCard";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userId",userId.toString()));
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new BankcardException();
        } catch (BankcardException b){
            throw new BankcardException();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public BankCard lookCard(Long userId) {
        if (ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/lookCard";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userId",userId.toString()));
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            String msg = jsonObject.getString("msg");
            String substring = msg.substring(1, msg.length()-1);
            net.sf.json.JSONObject cardObject = net.sf.json.JSONObject.fromObject(substring);
            BankCard bankCard =(BankCard) net.sf.json.JSONObject.toBean(cardObject, BankCard.class);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new BankcardException();
            return bankCard;
        } catch (BankcardException b){
            throw new BankcardException();
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<Map<String, Object>> list(Long userId) {
        if (ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String url = "http://localhost:6262/bank/list";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("userId",userId.toString()));
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            String map = jsonObject.getString("msg");
            List<Map<String, Object>> maps = ObjectUtil.toListMap(map);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new BankcardException();
            return maps;
        } catch (BankcardException b){
            throw new BankcardException();
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Long getThisCarMileage() {
        String url = "http://localhost:6262/bank/random";
        try{
            List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
            String result= HttpClientUtil.sendPost(url,nameValuePairs);
            logger.info("result: = "+result);
            JSONObject jsonObject= JSON.parseObject(result);
            String msg = jsonObject.getString("msg");
            Long mileage = Long.parseLong(msg);
            Integer code = Integer.valueOf(jsonObject.getString("code"));
            if (code != 0) throw new NumberErrorException();
            return mileage;
        } catch (NumberErrorException b){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        } catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
